<?php


class Model_Hr_Staffleave extends Zend_Db_Table_Abstract {

	protected $_name = 'staff_leave_case';
	
	public function getStaffleave( $idLeave){
		
		$row = $this->fetchRow("`id_leave` = ". $idLeave);
		if(!$row) return false;
		return $row->toArray();		
		
		}
		
	public function addStaffleave( $idStaff , $dateApply , $shopCode , $applicationDate,$periodType , $periodStart , $periodEnd , $periodStartHr , $periodStartMin , $periodEndHr ,$periodEndMin, $totalHrs , $leaveComm , $medicalProof , $approvedDate , $approvedIdStaff ,$approvedResult,$cancelReason, $hourAllocated , $paidDate){
		
		$data = array(
			
         "id_staff" =>  $idStaff ,
         "date_apply" =>  $dateApply ,
         "shop_code" =>  $shopCode ,
		 "application_date" => $applicationDate,
         "period_type" =>  $periodType ,
         "period_start" =>  $periodStart ,
         "period_end" =>  $periodEnd ,
         "period_start_hr" =>  $periodStartHr ,
         "period_start_min" =>  $periodStartMin ,
		 "period_end_hr" =>  $periodEndHr ,
		 "period_end_min" =>  $periodEndMin ,
         "total_hrs" =>  $totalHrs ,
         "leave_comm" =>  $leaveComm ,
         "medical_proof" =>  $medicalProof ,
         "approved_date" =>  $approvedDate ,
         "approved_id_staff" =>  $approvedIdStaff ,
		 "approved_result" => $approvedResult ,
		 "cancel_reason" => $cancelReason ,
         "hour_allocated" =>  $hourAllocated ,
         "paid_date" =>  $paidDate 
	
			);
		$this->insert($data);
		
		}
		
	public function updateStaffleave(  $idLeave ,$idStaff , $dateApply , $shopCode ,$applicationDate, $periodType , $periodStart , $periodEnd , $periodStartHr , $periodStartMin , $periodEndHr ,$periodEndMin, $totalHrs , $leaveComm , $medicalProof , $approvedDate , $approvedIdStaff ,$approvedResult,$cancelReason, $hourAllocated , $paidDate){
		$data = array(
			
         "id_staff" =>  $idStaff ,
         "date_apply" =>  $dateApply ,
         "shop_code" =>  $shopCode ,
		 "application_date" => $applicationDate,
         "period_type" =>  $periodType ,
         "period_start" =>  $periodStart ,
         "period_end" =>  $periodEnd ,
         "period_start_hr" =>  $periodStartHr ,
         "period_start_min" =>  $periodStartMin ,
		 "period_end_hr" =>  $periodEndHr ,
		 "period_end_min" =>  $periodEndMin ,
         "total_hrs" =>  $totalHrs ,
         "leave_comm" =>  $leaveComm ,
         "medical_proof" =>  $medicalProof ,
         "approved_date" =>  $approvedDate ,
         "approved_id_staff" =>  $approvedIdStaff ,
		 "approved_result" => $approvedResult ,
		 "cancel_reason" => $cancelReason ,
         "hour_allocated" =>  $hourAllocated ,
         "paid_date" =>  $paidDate 
	
			);
			
		$this->update($data,"`id_leave` = ". $idLeave);
		}

		/**
		 * Accept or cancel application, update result
		 * @version 2014-10-06
		 * @author Leo 
		 * @param unknown_type $idLeave
		 * @param unknown_type $approvedDate
		 * @param unknown_type $approvedIdStaff
		 * @param unknown_type $approvedResult
		 * @param unknown_type $cancelReason
		 */
		
	public function updateResult(  $idLeave ,$approvedDate , $approvedIdStaff ,$approvedResult,$cancelReason,$type){
			$data = array(
				 "approved_date" =>  $approvedDate ,
				 "approved_id_staff" =>  $approvedIdStaff ,
				 "approved_result" => $approvedResult ,
				 "cancel_reason" => $cancelReason,
				 "period_type" => $type
		
			);
				
			$this->update($data,"`id_leave` = ". $idLeave);
	}	
	
	public function updatePaidDate ($idLeave, $date){
		$data = array("paid_date" =>  $date);
		
		$this->update($data,"`id_leave` = ". $idLeave); 
	}
	
	public function updateNote ($idLeave, $note){
		$data = array("note" =>  $note);
		
		$this->update($data,"`id_leave` = ". $idLeave);
	}

	/**
	 * Get staff undeceided leave application by date
	 * @version 2014-10-07
	 * @author Leo
	 * @param unknown_type $stfId
	 * @param unknown_type $date
	 */
	public function getLeaveByIdDate($stfId,$date){
		$row = $this->fetchRow("`id_staff` = ".$stfId." AND `period_start` = '".$date."' AND `approved_result` is null");
		if(!$row) return false;
		return $row->toArray();
	}
	
	/**
	 * Get staff approve leave application by date
	 * @version 2014-10-08
	 * @author Leo
	 * @param unknown_type $stfId
	 * @param unknown_type $date
	 */
	public function getApproveLeaveByIdDate($stfId,$date){
		$row = $this->fetchRow("`id_staff` = ".$stfId." AND `period_start` = '".$date."' AND `approved_result` like 'accept'");
		if(!$row) return false;
		return $row->toArray();
	}
	
	public function getApproveLeaveByIdPeriod($stfId,$dateStart,$dateEnd){
		$row = $this->fetchAll("`id_staff` = ".$stfId." AND `period_start` >= '".$dateStart."' AND `period_start` <= '".$dateEnd."' AND `approved_result` like 'accept'","period_start ASC");
		if(!$row) return false;
		return $row->toArray();
	}
		
	public function deleteStaffleave( $idLeave){
		
		$this->delete("`id_leave` = ". $idLeave);
		
		}	
	
	public function listAll(){
		
		$rows =$this->fetchAll("1","id_leave DESC ");
		if(!$rows) return false;
		return $rows->toArray();			
		
		}
	/**
	 * List all unpaid annual leave list
	 * @version 2014-11-25
	 * @author Leo
	 * 
	 */
	public function listAllAcceptNoPaid(){
		$rows =$this->fetchAll("`approved_result` LIKE 'accept' AND `paid_date` is null","period_start ASC ");
		if(!$rows) return false;
		return $rows->toArray();
	}	
	
	public function listAllAcceptByPeriod($dateBegin,$dateEnd){
		$rows =$this->fetchAll("`approved_result` LIKE 'accept' AND `add_roster` is null AND `period_start` >= '".$dateBegin."' AND `period_start` <= '".$dateEnd."'","id_leave DESC ");
		if(!$rows) return false;
		return $rows->toArray();
	}
	
	public function listAllPending(){
		$rows =$this->fetchAll("`approved_result` is Null","id_leave DESC ");
		if(!$rows) return false;
		return $rows->toArray();
	}
	
	public function updateSpRoster($idLeave){
		$data = array("add_roster" =>  1);
		
		$this->update($data,"`id_leave` = ". $idLeave);
	}
	
	public function getPaidByStaff( $idStaff){
	
		$row = $this->fetchAll("`id_staff` = ". $idStaff." AND `paid_date` is not null","period_start DESC");
		if(!$row) return false;
		return $row->toArray();
	
	}
}


?>